#include <limits>
#include "MainManu.hh"
#include "Utils.hh"
#include <iostream>

namespace minibank {

// Constructor
MainManu::MainManu() {
}

// Copy constructor
MainManu::MainManu(const MainManu& other) {
}

// Copy assignment operator
MainManu& MainManu::operator=(const MainManu& other) {
    if (this != &other) {
    }
    return *this;
}

// Destructor
MainManu::~MainManu() {
}

// Display main menu
void MainManu::show() {
    std::cout << "===== Banking System =====" << std::endl;
    std::cout << "1. Register new customer" << std::endl;
    std::cout << "2. Access customer accounts" << std::endl;
    std::cout << "3. Display all bank accounts" << std::endl;
    std::cout << "4. Transaction reports" << std::endl;
    std::cout << "5. Exit" << std::endl;
    std::cout << "Please choose: ";
}

// Handle user input loop
void MainManu::handleInput() {
    int choice = 0;
    do {
        clearScreen(); // 添加清屏
        show();
        std::cin >> choice;
        switch (choice) {
            case 1:
                registerCustomer();
                break;
            case 2:
                accessCustomer();
                break;
            case 3:
                displayAllAccounts();
                break;
            case 4:
                showTransactionReports();
                break;
            case 5:
                std::cout << "Exiting system..." << std::endl;
                break;
            default:
                std::cout << "Invalid choice, try again." << std::endl;
        }
    } while (choice != 5);
}

// Register new customer
void MainManu::registerCustomer() {
    std::cout << "===== Register New Customer =====" << std::endl;
    std::cout << "Enter customer name: ";
    std::string name;
    std::cin.ignore();
    std::getline(std::cin, name);

    bankSystem_.registerCustomer(name);
    pause();
}

// Access customer by name
void MainManu::accessCustomer() {
    std::cout << "===== Access Customer Accounts =====" << std::endl;
    std::cout << "Enter customer name: ";
    std::string name;
    std::cin.ignore();
    std::getline(std::cin, name);

    const Customer* customer = bankSystem_.getCustomer(name);
    if (!customer) {
        std::cout << "Customer not found." << std::endl;
        pause();
        return;
    }

    showCustomerMenu(customer);
}

// Show customer submenu
void MainManu::showCustomerMenu(const Customer* customer) {
    int choice = 0;
    do {
        clearScreen();
        std::cout << "===== Customer: " << customer->getName() << " =====" << std::endl;
        std::cout << "This customer has the following accounts:\n" << std::endl;

        const BankAccount& savings = customer->getSavingsAccount();
        const BankAccount& checking = customer->getCheckingAccount();

        std::cout << "1. Savings Account: " << savings.accountId << std::endl;
        std::cout << "   Balance: ¥" << savings.balance << std::endl;

        std::cout << "\n2. Checking Account: " << checking.accountId << std::endl;
        std::cout << "   Balance: ¥" << checking.balance << std::endl;

        std::cout << "\nOptions:" << std::endl;
        std::cout << "1. Access Savings Account" << std::endl;
        std::cout << "2. Access Checking Account" << std::endl;
        std::cout << "3. View all transactions for this customer" << std::endl;
        std::cout << "4. Generate customer report" << std::endl;
        std::cout << "5. Return to main menu" << std::endl;
        std::cout << "Please choose: ";

        std::cin >> choice;
        std::cin.ignore();

        switch (choice) {
            case 1:
                bankSystem_.accessAccount(customer->getName(), AccountType::Savings);
                break;
            case 2:
                bankSystem_.accessAccount(customer->getName(), AccountType::Checking);
                break;
            case 3:
                bankSystem_.printCustomerTransactions(customer->getName());
                break;
            case 4:
                bankSystem_.generateCustomerReport(customer->getName());
                break;
            case 5:
                std::cout << "Returning to main menu...\n";
                break;
            default:
                std::cout << "Invalid choice. Please try again.\n";
        }

        pause();

    } while (choice != 5);
}

// Display all bank accounts
void MainManu::displayAllAccounts() {
    bankSystem_.displayAllAccounts();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    pause();
}

// Show transaction report menu
void MainManu::showTransactionReports() {
    int choice = 0;
    do {
        clearScreen();
        std::cout << "===== Transaction Reports =====" << std::endl;
        std::cout << "1. Generate customer report" << std::endl;
        std::cout << "2. Generate global report" << std::endl;
        std::cout << "3. Return to main menu" << std::endl;
        std::cout << "Please choose: ";
        std::cin >> choice;
        std::cin.ignore();

        switch (choice) {
            case 1: {
                std::cout << "Enter customer name: ";
                std::string name;
                std::getline(std::cin, name);
                bankSystem_.generateCustomerReport(name);
                pause();
                break;
            }
            case 2:
                bankSystem_.generateGlobalTransactionReport();
                pause();
                break;
            case 3:
                std::cout << "Returning to main menu...\n";
                break;
            default:
                std::cout << "Invalid choice. Please try again.\n";
                pause();
                break;
        }
    } while (choice != 3);
}

} // namespace minibank